package com.example.shaidemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

public class HushenAdapter extends RecyclerView.Adapter<HushenAdapter.ViewHolder> {
    private static final String TAG = "HushenAdapter";
    private List<Hushen> hushens;

    public HushenAdapter(List<Hushen> hushens) {
        this.hushens = hushens;
    }

    @NonNull
    @Override
    public HushenAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //完成创建ViewHolder和将布局用布局加载器加载到viewHolder中
//为viewHolder里面的子view注册监听事件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_hushen,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.hushen_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //viewHolder.getAdapterPosition()将会返回被点击的item的index
                int position = viewHolder.getAdapterPosition();
                Hushen hushen = hushens.get(position);
                //Toast.makeText(v.getContext(),"Click:"+hushen.getShai_type(),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(v.getContext(),HushenDetailActivity.class);
                v.getContext().startActivity(intent);

            }
        });
        viewHolder.hushen_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                Hushen hushen = hushens.get(position);
                Toast.makeText(v.getContext(),"审核",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(v.getContext(),HushenDetailActivity.class);
                v.getContext().startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull HushenAdapter.ViewHolder holder, int position) {
        //根据position将每一个flist里面的值绑定到viewholder
        Hushen item = hushens.get(position);


        holder.hushen_type.setText(item.getShai_type());
        holder.hushen_state.setText(item.getShenhe_state()+">");
        if(item.getShenhe_state()=="已审核"){
            holder.hushen_state.setTextColor(Color.GRAY);//系统自带的颜色类
        }
        if(item.getShai_type()=="薪资"){
            holder.massage1.setText("职 位："+item.getMassage1());
            holder.massage2.setText("公 司："+item.getCity()+"|"+item.getMassage2());
            holder.massage3.setText("薪 资：￥"+item.getMassage3());
        }
        else if(item.getShai_type()=="打车"){
            holder.massage1.setText("起 点："+item.getCity()+"|"+item.getMassage1());
            holder.massage2.setText("终 点："+item.getCity()+"|"+item.getMassage2());
            holder.massage3.setText("费 用：￥"+item.getMassage3());
        }
        else if(item.getShai_type()=="酒店") {
            holder.massage1.setText("地 点："+item.getCity()+"|"+item.getMassage1());
            holder.massage2.setText("时 长：" +item.getMassage2());
            holder.massage3.setText("费 用：￥" + item.getMassage3());
        }
    }

    @Override
    public int getItemCount() {
        return hushens.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        //这些控件都可以设置监听事件
        CardView hushen_list;
        TextView hushen_type;
        TextView hushen_state;
        TextView massage1;
        TextView massage2;
        TextView massage3;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            hushen_list = itemView.findViewById(R.id.card_hushen);
            hushen_type = itemView.findViewById(R.id.hushen_type);
            hushen_state = itemView.findViewById(R.id.hushen_state);
            massage1 = itemView.findViewById(R.id.massage1);
            massage2 = itemView.findViewById(R.id.massage2);
            massage3 = itemView.findViewById(R.id.massage3);
        }
    }

/*
    public HushenAdapter(@NonNull Context context, int resource, @NonNull List<Hushen> list) {
        super(context, resource, list);
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView=convertView;
        if(itemView==null){
            itemView= LayoutInflater.from(getContext()).inflate(R.layout.list_item_hushen,parent,
                    false);
        }
        Hushen item=(Hushen) getItem(position);
        TextView hushen_type=(TextView)itemView.findViewById(R.id.hushen_type);
        Log.i(TAG, "adapter:hushen_type=" + hushen_type);
        TextView hushen_state=(TextView)itemView.findViewById(R.id.hushen_state);
        Log.i(TAG, "adapter:hushen_state=" + hushen_state);
        TextView massage1=(TextView)itemView.findViewById(R.id.massage1);
        Log.i(TAG, "adapter:massage1=" + massage1);
        TextView massage2=(TextView)itemView.findViewById(R.id.massage2);
        Log.i(TAG, "adapter:massage2=" + massage2);

        TextView massage3=(TextView)itemView.findViewById(R.id.massage3);
        Log.i(TAG, "adapter:massage3=" + massage3);

        //显示

        hushen_type.setText(item.getShai_type());
        hushen_state.setText(item.getShenhe_state()+">");
        if(item.getShai_type()=="薪资"){
            massage1.setText("职 位："+item.getMassage1());
            massage2.setText("公 司："+item.getCity()+"|"+item.getMassage2());
            massage3.setText("薪 资：￥"+item.getMassage3());
        }
        else if(item.getShai_type()=="打车"){
            massage1.setText("起 点"+item.getCity()+"|"+item.getMassage1());
            massage2.setText("终 点"+item.getCity()+"|"+item.getMassage2());

            massage3.setText("费 用：￥"+item.getMassage3());
        }
        else if(item.getShai_type()=="酒店") {
            massage1.setText("地 点："+item.getCity()+"|"+item.getMassage1());
            massage2.setText("时 长：" +item.getMassage2());

            massage3.setText("费 用：￥" + item.getMassage3());
        }

        return itemView;
    }
*/

}
